package cn.shiva.redisunit.message.stream;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.stream.StreamListener;
import org.springframework.stereotype.Component;

/**
 * @author shiva   2022-08-16 21:11
 */
@Slf4j
@Component
public class MessageStreamListener implements StreamListener<String, MapRecord<String, String, String>> {
    @Autowired
    private RedisTemplate redisTemplate;
    //只会接收到新消息，就算历史的消息没有删除，也不会接收
    @Override
    public void onMessage(MapRecord<String, String, String> message) {
        log.info("接受到来自redis的消息");
        System.out.println("message id " + message.getId());
        System.out.println("stream " + message.getStream());
        System.out.println("body " + message.getValue());
        //根据id,删除stream里的消息
        redisTemplate.opsForStream().delete("mystream", message.getId());
    }
}
